package com.ruoyi.pos.service.impl;

import com.ruoyi.pos.domain.*;
import com.ruoyi.pos.mapper.*;
import com.ruoyi.pos.service.IPosDingdanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class PosDingdanServiceImpl implements IPosDingdanService {
    @Autowired
    private PosDingdanMapper posDingdanMapper;
    @Autowired
    private PosDingdanInfoMapper posDingdanInfoMapper;
    @Autowired
    private PosCheMapper posCheMapper;
    @Autowired
    private PosDingdanItemMapper posDingdanItemMapper;
    @Autowired
    private PosShangpinMapper posShangpinMapper;
    @Override
    public int insertPosDingdan(PosDingdan posDingdan) {
        return posDingdanMapper.insertPosDingdan(posDingdan);
    }

    @Override
    public Integer getPosDingdanById(String dingdanCard) {
        return posDingdanMapper.getPosDingdanById(dingdanCard);
    }

    @Override
    public List<PosDingdan> selectMyPosDingDan(Integer customerId) {
        return posDingdanMapper.selectMyPosDingDan(customerId);
    }

    @Override
    @Transactional
    public boolean insertPosDingDan(PosDingdanInfo posDingdanInfo) {
        List<PosDingdanInfo> posDingdanInfoList = posDingdanInfoMapper.selectPosDingdanInfoList(posDingdanInfo);
        // 获取当前时间并格式化
        String now = LocalDateTime.now().format(DATE_TIME_FORMATTER);
        // 生成四位随机数，不足四位前面补0
        String randomPart = String.format("%04d", RANDOM.nextInt(10000));
        // 拼接成最终的订单号
        String dingdanNumber = now + randomPart;
        PosDingdan posDingdan = new PosDingdan();
        posDingdan.setDingdanCard(dingdanNumber);
        posDingdan.setRealName(posDingdanInfoList.get(0).getRealName());
        posDingdan.setPhone(posDingdanInfoList.get(0).getPhone());
        posDingdan.setAddress(posDingdanInfoList.get(0).getAddress());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = sdf.format(date);
        posDingdan.setCreateTime(dateTime);
        posDingdan.setPayWay(posDingdanInfo.getPayWay());
        posDingdan.setCustomerId(posDingdanInfo.getCustomerId());
        posDingdanMapper.insertPosDingdan(posDingdan);
        Integer dingdanId = posDingdanMapper.getPosDingdanById(dingdanNumber);
        PosChe posChe = new PosChe();
        posChe.setCustomerId(posDingdanInfo.getCustomerId());
        List<PosChe> posCheList = posCheMapper.selectPosCheList(posChe);
        List<PosDingdanItem> posDingdanItemList = new ArrayList<>();
        String[] posCheIdString = posDingdanInfo.getPosCheId().split(",");
        int[] posCheIdInt = new int[posCheIdString.length];
        for(int i = 0; i < posCheIdString.length; i++){
            posCheIdInt[i] = Integer.parseInt(posCheIdString[i]);
        }
        for(PosChe pc : posCheList){
            for(int i = 0; i < posCheIdInt.length; i++){
                if(pc.getId() == posCheIdInt[i]){
                    PosDingdanItem posDingdanItem = new PosDingdanItem();
                    posDingdanItem.setMendianId(pc.getMendianId());
                    posDingdanItem.setShangpinId(pc.getShangpinId());
                    posDingdanItem.setColourId(pc.getColourId());
                    posDingdanItem.setSizeId(pc.getSizeId());
                    posDingdanItem.setGoodsCount(pc.getGoodsCount());
                    posDingdanItem.setHarvestWay(pc.getHarvestWay());
                    posDingdanItem.setDingdanId(dingdanId);
                    posDingdanItemList.add(posDingdanItem);
                    break;
                }
            }

        }
        for(PosDingdanItem posDingdanItem : posDingdanItemList){
            posDingdanItemMapper.insertPosDingdanItem(posDingdanItem);
            PosShangpinItem posShangpinItem = new PosShangpinItem();
            posShangpinItem.setShangpinId(posDingdanItem.getShangpinId());
            posShangpinItem.setColourId(posDingdanItem.getColourId());
            posShangpinItem.setSizeId(posDingdanItem.getSizeId());
            PosShangpinItem posShangpinItem1 = posShangpinMapper.selectPosShangpinItemList(posShangpinItem).get(0);
            posShangpinItem1.setGoodsCount(posShangpinItem1.getGoodsCount()-posDingdanItem.getGoodsCount());
            posShangpinMapper.updatePosShangpinItem(posShangpinItem1);
        }
        for(PosChe posChe1 : posCheList){
            for(int i = 0; i < posCheIdInt.length; i++){
                if(posChe1.getId() == posCheIdInt[i]){
                    posCheMapper.deletePosCheById(posChe1.getId());
                    break;
                }
            }
        }
        return true;
    }

    @Override
    public PosDingdan selectPosDingdanById(Integer id) {
        return posDingdanMapper.selectPosDingdanById(id);
    }

    @Override
    @Transactional
    public int deletePosDingdanAndItem(Integer id, Integer dingdanId) {
        int i = posDingdanItemMapper.deletePosDingdanItem(id);
        PosDingdanItem posDingdanItem = new PosDingdanItem();
        posDingdanItem.setDingdanId(dingdanId);
        List<PosDingdanItem> posDingdanItemList = posDingdanItemMapper.selectPosDingdanItem(posDingdanItem);
        if(posDingdanItemList == null || posDingdanItemList.size() == 0){
            i = posDingdanMapper.deletePosDingdan(dingdanId);
        }
        return i;
    }

    private static final Random RANDOM = new Random();
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
}
